<?php
/**
 * @Author: Wang chunsheng  email:2192138785@qq.com
 * @Date:   2021-06-02 17:20:53
 * @Last Modified by:   Wang chunsheng  email:2192138785@qq.com
 * @Last Modified time: 2021-06-04 14:22:33
 */
 

namespace admin\controllers\addons;

use Yii;
use diandi\addons\models\Bloc;
use admin\models\addons\models\Bloc as BlocSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use backend\controllers\BaseController;
use admin\controllers\AController;
use admin\models\enums\BlocStatus;
use admin\models\enums\LevelStatus;
use admin\models\enums\ReglevelStatus;
use common\helpers\ArrayHelper;
use common\helpers\ResultHelper;
use common\helpers\ErrorsHelper;
use diandi\addons\models\BlocLevel;
use diandi\addons\models\BlocStore;


/**
 * BlocController implements the CRUD actions for Bloc model.
 */
class BlocController extends AController
{
    public $modelSearchName = "BlocSearch";
    
    public $modelClass = '';
    
    public $extras = [];
    

    /**
     * Lists all Bloc models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new BlocSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return ResultHelper::json(200, '获取成功',[
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Bloc model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id)
    {

        $view = $this->findModel($id);
        $view['extra'] = unserialize($view['extra']);

        return ResultHelper::json(200, '获取成功', $view);

    }

    /**
     * Creates a new Bloc model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Bloc();
        
        if (Yii::$app->request->isPost) {
            $data = Yii::$app->request->post();
            
            $data['province'] = $data['provinceCityDistrict']['0'];
            $data['city'] = $data['provinceCityDistrict']['1'];
            $data['district'] = $data['provinceCityDistrict']['2'];
            
            
            
            if ($model->load($data, '') && $model->save()) {
               
                return ResultHelper::json(200, '创建成功', $model);
            } else {
                $msg = ErrorsHelper::getModelError($model);
                return ResultHelper::json(400, $msg);
            }
        }
        
    }

    /**
     * Updates an existing Bloc model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if (Yii::$app->request->isPut) {
            $data = Yii::$app->request->post();

            if ($model->load($data, '') && $model->save()) {
               
                return ResultHelper::json(200, '编辑成功', $model);
            } else {
                $msg = ErrorsHelper::getModelError($model);
                return ResultHelper::json(400, $msg);
            }
        }
        
    }

    /**
     * Deletes an existing Bloc model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return ResultHelper::json(200, '删除成功');
    }

    /**
     * Finds the Bloc model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Bloc the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Bloc::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }

    /**
     * Creates a new Bloc model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     *
     * @return mixed
     */
    public function actionParentbloc()
    {
        $model = new Bloc([
            'extras' => $this->extras,
            ]);
            
        $parents = $model->find()->select(["bloc_id",'pid','business_name as label','bloc_id as id'])->asArray()->all();
            
        $parentBloc =  ArrayHelper::itemsMerge($parents,0,"bloc_id",'pid','children');

        return ResultHelper::json(200, '获取成功',$parentBloc);
    }

    /**
     * Updates an existing Bloc model.
     * If update is successful, the browser will be redirected to the 'view' page.
     *
     * @param int $id
     *
     * @return mixed
     *
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionStores($id)
    {
     
        $stores = BlocStore::find()->where(['bloc_id'=>$id])->asArray()->all();
       
        return ResultHelper::json(200, '获取成功',$stores);
     
    }

    

    public function actionReglevel()
    {
        $list = ReglevelStatus::listData();
        
        $lists = [];

        foreach ($list as $key => $value) {
            $lists[] = [
                "text" => $value,
                "value"=> $key
            ];
        }
        
        return ResultHelper::json(200, '获取成功',$lists);
    }

    public function actionBlocstatus()
    {
        
        $list = BlocStatus::listData();
        
        $lists = [];

        foreach ($list as $key => $value) {
            $lists[] = [
                "text" => $value,
                "value"=> $key
            ];
        }
        
        return ResultHelper::json(200, '获取成功',$lists);
        
    }


    public function actionLevels()
    {
        global $_GPC;
        
        $global_bloc_id = Yii::$app->params['global_bloc_id'];
   
        $levels = BlocLevel::find()->where(['global_bloc_id'=>$global_bloc_id])->select(['level_num as value','name as text'])->asArray()->all();
        
        return ResultHelper::json(200, '获取成功',$levels);
    
    }
    
}
